Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Nested If

Nested If - Example - 1

Here are the few examples for Nested If statements which helps you to understand basic syntax and concepts.

Example 1: Nested if for Grading

Grading system example in java
public class Main{ public static void main(String[] args) { int marks = 75; // Check if marks are greater than or equal to 60 if (marks >= 60) { // If marks are 90 or higher, print Grade A if (marks >= 90) { System.out.println("Grade: A"); } // If marks are between 80 and 89, print Grade B else if (marks >= 80) { System.out.println("Grade: B"); } // If marks are between 70 and 79, print Grade C else if (marks >= 70) { System.out.println("Grade: C"); } // If marks are between 60 and 69, print Grade D else { System.out.println("Grade: D"); } } // If marks are less than 60, print Grade F else { System.out.println("Grade: F"); } } }

Output

Grade: C
Inside this example, an int variable named marks is declared and assigned a value of 75. This variable represents the marks or score obtained by a student. The code then enters a series of nested if statements to determine the grade based on the value of marks: The outermost if statement checks if marks are greater than or equal to 60. If this condition is true, the program proceeds to evaluate the specific grade. Inside the first if block, there are multiple else if blocks that further check the value of marks: If marks are 90 or higher, the program prints "Grade: A". If marks are between 80 and 89 (inclusive), the program prints "Grade: B". If marks are between 70 and 79 (inclusive), the program prints "Grade: C". If marks are between 60 and 69 (inclusive), the program prints "Grade: D". If none of the conditions in the inner if statements are met (i.e., if marks are less than 60), the program skips the entire inner if structure and directly moves to the else block of the outermost if statement. Here, it prints "Grade: F". In summary, the code determines the student's grade based on their marks as follows: If marks are 90 or higher: "Grade: A" If marks are between 80 and 89: "Grade: B" If marks are between 70 and 79: "Grade: C" If marks are between 60 and 69: "Grade: D" If marks are less than 60: "Grade: F"

Example 2: Greeting Message

Greeting example in java
public class Main{ public static void main(String[] args) { int hour = 15; // Check if it's before 12 (morning) if (hour < 12) { System.out.println("Good morning!"); } else { // If not morning, check if it's before 18 (afternoon) if (hour < 18) { System.out.println("Good afternoon!"); } else { // If neither morning nor afternoon, it's evening System.out.println("Good evening!"); } } } }

Output

Good afternoon!
In this example,The code starts by declaring an int variable named hour and assigning it a value of 15. This value represents the hour of the day (in 24-hour format). The code then enters a series of nested if statements to determine the appropriate greeting based on the value of hour: The first if statement checks if the hour is less than 12. If this condition is true, it means that the time is before 12:00 (morning), and the message "Good morning!" is printed. If the hour is not less than 12, the code moves to the else block. Here, another if statement checks if the hour is less than 18. If this condition is true, it means that the time is between 12:00 and 18:00 (afternoon), and the message "Good afternoon!" is printed. If none of the above conditions are met, it means that the time is 18:00 or later (evening), and the message "Good evening!" is printed. So, depending on the value of hour, the program will output one of the following messages: If hour is less than 12: "Good morning!" If hour is between 12 and 18: "Good afternoon!" If hour is 18 or later: "Good evening!"

Example 3: Nested if for Age Categories

Age category finder example in java
public class Main{ public static void main(String[] args) { int age = 35; char gender = 'F'; // Check if the age is greater than or equal to 18 if (age >= 18) { // If age is 18 or older, check the gender if (gender == 'M') { System.out.println("You are a male adult."); } else { System.out.println("You are a female adult."); } } else { // If age is less than 18, check the gender if (gender == 'M') { System.out.println("You are a male minor."); } else { System.out.println("You are a female minor."); } } } }

Output

You are a female adult.
In this example, The code begins by declaring a variable age and assigning it a value of 35, which represents the age of the person. Another variable gender is declared and assigned a value of 'F', which represents the gender of the person (in this case, female). The code then enters a series of nested if statements to determine the appropriate message to print based on the person's age and gender: The outermost if statement checks whether the age is greater than or equal to 18, which indicates whether the person is an adult. If the person is an adult, the nested if statements check the gender to determine whether the person is male or female, and the corresponding message is printed. If the person is not an adult (age is less than 18), the code goes into the else block of the outermost if statement. Here again, the nested if statements check the gender to determine whether the person is a male or female minor, and the corresponding message is printed. So, depending on the values of age and gender, the program will output one of the following messages: If age is greater than or equal to 18 and gender is 'M': "You are a male adult." If age is greater than or equal to 18 and gender is 'F': "You are a female adult." If age is less than 18 and gender is 'M': "You are a male minor." If age is less than 18 and gender is 'F': "You are a female minor."

Example 4: Nested if for Ticket Pricing

Ticket price category in java
public class Main{ public static void main(String[] args) { int age = 25; boolean isStudent = true; if (age < 18) { // The user is a child or a student. if (isStudent) { // The user is a student. System.out.println("Student discount applies."); } else { // The user is a child. System.out.println("Child ticket price applies."); } } else { // The user is an adult. System.out.println("Adult ticket price applies."); } } }

Output

Adult ticket price applies.
In this example, The code first declares two variables: age and isStudent. The age variable stores the user's age, and the isStudent variable stores a boolean value that indicates whether the user is a student. The code then uses a nested if statement to determine the ticket price that applies to the user. The outer if statement checks if the user's age is less than 18. If it is, then the inner if statement is evaluated. The inner if statement checks if the user is a student. If the user is a student, then the message "Student discount applies" is printed to the console. Otherwise, the message "Child ticket price applies" is printed to the console. If the user's age is not less than 18, then the outer if statement is skipped. In this case, the message "Adult ticket price applies" is printed to the console.

  📌TAGS

★If else condition ★java ★java if ★if else ★nested if else ★nested if ★conditional statements

Tutorials